package db

import (
	"database/sql"
	"pmservice/graph/model"

	gonanoid "github.com/matoous/go-nanoid/v2"
	"gorm.io/gorm"
)

type Ecm struct {
	gorm.Model
	UID       string
	Hospital  string
	Code      int
	DevModel  string
	IpAddress sql.NullString
}

func (Ecm) TableName() string {
	return "ecm"
}

func (d *Ecm) FromGQLNew(in *model.EcmNew) {
	d.Hospital = in.Hosuid
	d.Code = in.Code
	d.DevModel = in.Model
	if in.Ipaddress != nil {
		d.IpAddress = sql.NullString{Valid: true, String: *in.Ipaddress}
	}
}

func (d *Ecm) FromGQLEdit(in *model.EcmEdit) []string {
	fields := []string{}
	if in.Code != nil {
		d.Code = *in.Code
		fields = append(fields, "code")
	}

	if in.Model != nil {
		d.DevModel = *in.Model
		fields = append(fields, "dev_model")
	}

	if in.Ipaddress != nil {
		d.IpAddress = sql.NullString{Valid: true, String: *in.Ipaddress}
		fields = append(fields, "ip_address")
	}

	return fields
}

func (d *Ecm) ToGQL() *model.ElectroCardiacMonitor {
	ecm := &model.ElectroCardiacMonitor{
		UID:  d.UID,
		Code: d.Code,
	}
	if d.IpAddress.Valid {
		ecm.Ipaddress = &d.IpAddress.String
	}
	return ecm
}

func EcmGet(dbs *gorm.DB, uid string) *Ecm {
	d := Ecm{}
	result := dbs.Where("uid=?", uid).Take(&d)
	if result.Error != nil {
		return nil
	}
	return &d
}

func EcmGetByCode(dbs *gorm.DB, hosuid string, code int) *Ecm {
	d := Ecm{}
	result := dbs.Where("hospital=? AND code=?", hosuid, code).Take(&d)
	if result.Error != nil {
		return nil
	}
	return &d
}

func EcmGetByIpaddress(dbs *gorm.DB, ipaddress string) *Ecm {
	d := Ecm{}
	result := dbs.Where("ip_address=?", ipaddress).Take(&d)
	if result.Error != nil {
		return nil
	}
	return &d
}

// 检查账号名是否已被使用, except:忽略自己
func EcmExist(dbs *gorm.DB, hosuid string, except string, code int) bool {
	d := Ecm{}
	result := dbs
	if except == "" {
		result = dbs.Where("code=? AND hospital=?", code, hosuid).Take(&d)
	} else {
		result = dbs.Where("code=? AND hospital=? AND uid!=?", code, hosuid, except).Take(&d)
	}
	return result.Error == nil && result.RowsAffected > 0
}

func EcmNew(dbs *gorm.DB, input *model.EcmNew) (*Ecm, error) {
	d := Ecm{
		UID: gonanoid.Must(),
	}
	d.FromGQLNew(input)
	// 创建账号
	result := dbs.Create(&d)
	if result.Error != nil {
		return nil, result.Error
	}
	return &d, nil
}

func EcmSave(dbs *gorm.DB, hosuid string, uid string, input *model.EcmEdit) bool {
	d := Ecm{}
	fields := d.FromGQLEdit(input)
	result := dbs.Where("uid=? AND hospital=?", uid, hosuid).
		Select(fields).
		Updates(&d)
	if result.Error != nil || result.RowsAffected == 0 {
		return false
	}
	return true
}

func EcmDel(dbs *gorm.DB, uid string) bool {
	result := dbs.Where("uid=?", uid).Delete(&Ecm{})
	return result.Error == nil && result.RowsAffected > 0
}

func EcmSearch(dbs *gorm.DB, cond model.EcmSearchCond) (ecms []*Ecm, total int64) {
	if cond.Facuid != nil {
		return FacGetByInstalledEcm(dbs, cond)
	}

	result := dbs.Where("hospital=?", cond.Hosuid)
	result.Model(&Ecm{}).Count(&total)
	if cond.Page != nil {
		result = result.Order("id DESC").Limit(cond.Page.Number).Offset(cond.Page.Offset).Find(&ecms)
	} else {
		result = result.Order("id DESC").Find(&ecms)
	}

	if result.Error != nil {
		return nil, 0
	}

	return ecms, total
}

func EcmAll(dbs *gorm.DB) (ecms []*Ecm) {
	result := dbs
	result = result.Order("id DESC").Find(&ecms)
	if result.Error != nil {
		return nil
	}
	return ecms
}
